﻿using System;
using System.Collections.Generic;
using System.Linq;
using Best.SmartCms.Common;
using Best.SmartCms.Dao.Common;
using Best.SmartCms.Domain;
using Best.SmartCms.Domain.Security;
using Best.SmartCms.Dto.Request;
using Best.SmartCms.Dto.User;

namespace Best.SmartCms.Dao
{
    /// <summary>
    /// 用户
    /// </summary>
    public class UserDao : BaseDao<User>, ICommonDao
    {
        private readonly MySqlDbContext _db;

        public UserDao(MySqlDbContext dbContext) : base(dbContext)
        {
            _db = dbContext;
        }


        public User FindById(long id)
        {
            return _db.UserSet.FirstOrDefault(p => p.Id == id && p.Enabled);
        }

        public User FindEnabledUserByPhone(string phone)
        {
            return _db.UserSet.FirstOrDefault(p => p.Phone == phone && p.Enabled);
        }

        public User FindByPhone(string phone)
        {
            return _db.UserSet.FirstOrDefault(p => p.Phone == phone);
        }


        public List<User> PageList(UserListQueryDto query, out int totalCount, out int totalPages)
        {
            var result = _db.UserSet.AsQueryable();
            if (!string.IsNullOrEmpty(query.Keywords))
            {
                result = result.Where(d => d.Name.Contains(query.Keywords) || d.Code.Contains(query.Keywords));
            }

            if (!string.IsNullOrEmpty(query.Phone))
            {
                result = result.Where(d => d.Phone == query.Phone);
            }

            if (query.Enabled != null)
            {
                result = result.Where(d => d.Enabled==query.Enabled.GetValueOrDefault());
            }

            // 排序
            if (string.IsNullOrEmpty(query.SortField))
            {
                result = result.OrderByDescending(p => p.CreatedTime);
            }
            else
            {
                if (query.SortField.Equals("mail", StringComparison.CurrentCultureIgnoreCase))
                {
                    result = query.SortOrder.Equals(SortOrder.Ascend.ToString(), StringComparison.CurrentCultureIgnoreCase) ? result.OrderBy(p => p.Mail) : result.OrderByDescending(p => p.Mail);
                }
                else if (query.SortField.Equals("Phone", StringComparison.CurrentCultureIgnoreCase))
                {
                    result = query.SortOrder.Equals(SortOrder.Ascend.ToString(), StringComparison.CurrentCultureIgnoreCase) ? result.OrderBy(p => p.Phone) : result.OrderByDescending(p => p.Phone);

                }
                else if (query.SortField.Equals("Name", StringComparison.CurrentCultureIgnoreCase))
                {
                    result = query.SortOrder.Equals(SortOrder.Ascend.ToString(), StringComparison.CurrentCultureIgnoreCase) ? result.OrderBy(p => p.Name) : result.OrderByDescending(p => p.Name);
                }
            }

            return result.PageBy(query.PageIndex, query.PageSize, out totalPages, out totalCount)
                .ToList();

        }
    }
}
